Using a Redis Hash

Description

Redis hash is a key that contains its own 'keys' called 'fields'.

In the following example, we create a hash called 'HiScore' that has fields that match names, each of which contain a numeric value.

The HSet method is used to set a field in a hash to a value

dim redis as extension::RedisClient = extension::RedisClient::CreateClient()
dim hash as extension::RedisResult

redis.HSet("HiScore","John",100)
redis.HSet("HiScore","Tim",120)

The HExists method returns a valueInteger of '1' if a field exists, otherwise a value of '0' is returned.

? redis.HExists("HiScore","John").valueInteger
= 1
? redis.HExists("HiScore","Jim").valueInteger
= 0

The HGetAll method returns an array of interleaved key/value pairs.

hash = redis.HGetAll("HiScore")
? hash.type
= "Array"

? hash.valueString
= John
100
Tim
120

redis.HSet("HiScore","John",130)
hash = redis.HGetAll("HiScore")
? hash.valueString
= John
130
Tim
120

The HKeys method returns just the fields (keys).

hash = redis.HKeys("HiScore")
? hash.valueString
= John
Tim

The HVals method returns just the values.

hash = redis.HVals("HiScore")
? hash.valueString
= 130
120

The HDel method removes a field from the hash

redis.HDel("HiScore","John")

hash = redis.HKeys("HiScore")
? hash.valueString
= "Tim"